﻿using BomDiffAnalyst.Views;
using BomDiffAnalyst.ViewModels;
using MahApps.Metro.Controls;
using Prism.Unity;
using Prism.Ioc;
using Prism.Modularity;
using Prism.Navigation.Regions;
using Prism.Events;
using System.Windows;
using Unity;
using Module.Common.EventAggregator;
using AvalonDock;
using Module.Common.CustomerRegionAdapters;
using Prism.AvalonDock.Extensions.Regions;
using log.Nlog.ILogger;
using log.Nlog.Logger;

namespace BomDiffAnalyst
{


    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : PrismApplication
    {

        private EventAggregator _ea;
        private bool _showMainwindow = false;

        private const string ModulePath = @".\Module";
        private const string MODULES_PATH = @".\";
 
        /* Prism applications startup in the following order:
        *
        * 01 CreateContainerExtension
        * 02 CreateModuleCatalog
        * 03 RegisterRequiredTypes
        * 04 RegisterSingletons
        * 05 ConfigureModuleCatalog
        * 06 ConfigureRegionAdapterMappings
        * 07 ConfigureDefaultRegionBehaviours
        * 08 RegisterFrameworkExceptionTypes
        * 09 CreateShell
        * 10 InitializeShell
        * 11 InitializeModules
        *
        * Initialization sequence is triggered by the OnStartup event.
        * See https://github.com/PrismLibrary/Prism/blob/master/src/Wpf/Prism.Wpf/PrismApplicationBase.cs for more info.
        */

        //依赖注入

        protected override void RegisterTypes(IContainerRegistry containerRegistry)
        {
            _ea = new EventAggregator();
            _ea.GetEvent<LoginSentEvent>().Subscribe(MessageReceived);

            //注册导航
            containerRegistry.RegisterForNavigation<NotificationDialog, TestViewModel>();

            containerRegistry.RegisterForNavigation<SkinView, SkinViewModel>();

            //注册logger
            containerRegistry.RegisterSingleton<ILoggerService, NLogLoggerService>();

            //注册消息订阅
            containerRegistry.RegisterInstance(_ea);
        }

    
        protected override Window CreateShell()
        {
            //UI线程未捕获异常处理事件
            //this.DispatcherUnhandledException += OnDispatcherUnhandledException;
            //Task线程内未捕获异常处理事件
            //TaskScheduler.UnobservedTaskException += OnUnobservedTaskException;
            //多线程异常
            //AppDomain.CurrentDomain.UnhandledException += OnUnhandledException;
            return Container.Resolve<MainWindow>();

        }


        protected override void OnInitialized( )
        {

            //Container.Resolve<Login>().ShowDialog();

            //var login = Container.Resolve<Login>();
            //var loginResult = login.ShowDialog();
            //if (loginResult.Value)
            //    base.OnInitialized();
            //else
            //    Current.Shutdown();

            base.OnInitialized();
        }


        /// <summary>
        /// 根据文件路径加载模块
        /// </summary>
        /// <returns></returns>
        //
        //02 CreateModuleCatalog
        protected override IModuleCatalog CreateModuleCatalog()
        {
            //if (!Directory.Exists(modulePath))
            //{
            //    Directory.CreateDirectory(modulePath);
            //}

            //指定模块加载方式为从文件夹中以反射发现并加载module(推荐用法)
            return new DirectoryModuleCatalog() { ModulePath = ModulePath };
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="moduleCatalog"></param>

        protected override void ConfigureModuleCatalog(IModuleCatalog moduleCatalog)
        {

        }

        protected override void ConfigureRegionAdapterMappings(RegionAdapterMappings regionAdapterMappings)
        {
            base.ConfigureRegionAdapterMappings(regionAdapterMappings);

            // Custom Adapters
            regionAdapterMappings.RegisterMapping(typeof(DockingManager), Container.Resolve<DockingManagerRegionAdapter>());
            regionAdapterMappings.RegisterMapping(typeof(HamburgerMenuItemCollection), Container.Resolve<MahAppsHamburgerMenuItemCollectionRegionAdapter>());
            regionAdapterMappings.RegisterMapping(typeof(HamburgerMenu), Container.Resolve<MahAppsHamburgerMenuRegionAdapter>());

        }




        private void MessageReceived(bool loginResult)
        {
            //Current.MainWindow.Hide();
            //base.OnInitialized();
            //ShowMainwindow = loginResult;
        }

    }
}
